package com.gm.wholesale.module.setting.ui;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.gm.commonlib.base.baseAdapter.BaseCommAdapter;
import com.gm.commonlib.base.baseui.baseFragment.BaseFragment;
import com.gm.wholesale.R;
import com.gm.wholesale.constant.SettingItemMenu;
import com.gm.wholesale.module.config.ConfigData;
import com.gm.wholesale.module.setting.bean.LeftItemBean;
import com.gm.wholesale.module.setting.ui.fragment.OrderPrintBlueListFragment;
import com.gm.wholesale.module.setting.ui.fragment.OrderPrintTemplateListFragment;
import com.gm.wholesale.module.setting.ui.fragment.ReceiptBlueListFragment;
import com.gm.wholesale.module.setting.ui.fragment.ReceiptTemplateListFragment;
import com.gm.wholesale.module.setting.ui.fragment.SetGoodPriceFragment;
import com.gm.wholesale.module.setting.ui.fragment.WeightBlueListFragment;
import com.gm.wholesale.module.setting.view.SettingLeftItemView;
import com.gm.wholesale.util.AppUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SettingFragment extends BaseFragment  {
    private View mMainView;
    private TextView versionName;
    private HashMap<Integer,Fragment> mFragments = new HashMap<>();
    private OrderPrintBlueListFragment mOrderPrintBlueListFragment; //打印机蓝牙
    private OrderPrintTemplateListFragment mOrderPrintTemplateListFragment; //打印机模板

    private ReceiptBlueListFragment mReceiptBlueListFragment; //小票打印蓝牙

    private ReceiptTemplateListFragment mReceiptTemplateListFragment;//小票打印模板
    private WeightBlueListFragment mWeightBlueListFragment; //称重蓝牙
    private SetGoodPriceFragment mSetGoodPriceFragment; //商品价格设置
    private ListView mSettingListview;
    private List<LeftItemBean> mItemData = new ArrayList<>();
    private List<SettingLeftItemView> settingLeftItemViews = new ArrayList<>();

    private FragmentManager mFragmentManager ;
    private FragmentTransaction mFragmentTransaction;

    @Override
    protected void onCreateContentBody(ViewGroup parent) {

        mMainView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_setting, parent);
        initView();
        initData();
    }

    private void initData() {
        mFragments.clear();
        versionName.setText("版本:v" + AppUtil.getVersionName(getActivity()));
        // todo 左边 item 项目
        for (SettingItemMenu settingItemMenu : SettingItemMenu.values()) {
            if (settingItemMenu.getType()==SettingItemMenu.WEIGHT_CONNECTION.getType()){
                if (!ConfigData.getConfigData().hasCreateOrderPermission()){
                      continue;
                }
            }
            mItemData.add(new LeftItemBean(settingItemMenu.getType(),settingItemMenu.getName()));
        }
        mFragments.put(0,new Fragment()); //设置
        mOrderPrintBlueListFragment = new OrderPrintBlueListFragment(); //打印机
        mFragments.put(SettingItemMenu.PRINT_CONNECTION.getType(),mOrderPrintBlueListFragment);

        mOrderPrintTemplateListFragment = new OrderPrintTemplateListFragment();//打印机模板
        mOrderPrintTemplateListFragment.setIsGetData(true);
        mFragments.put(SettingItemMenu.PRINT_TEMP.getType(),mOrderPrintTemplateListFragment);

        mReceiptBlueListFragment=new ReceiptBlueListFragment(); //小票打印机
        mFragments.put(SettingItemMenu.PRINT_RECEIPT_CONNECTION.getType(),mReceiptBlueListFragment);

        mReceiptTemplateListFragment=new ReceiptTemplateListFragment();//小票打印模板
        mReceiptTemplateListFragment.setIsGetData(true);
        mFragments.put(SettingItemMenu.PRINT_RECEIPT_TEMP.getType(), mReceiptTemplateListFragment);
        //有开单权限才有蓝牙秤
        if (ConfigData.getConfigData().hasCreateOrderPermission()){
            mWeightBlueListFragment = new WeightBlueListFragment(); //蓝牙秤
            mFragments.put(SettingItemMenu.WEIGHT_CONNECTION.getType(), mWeightBlueListFragment);
        }
        mSetGoodPriceFragment = new SetGoodPriceFragment(); //商品价格
        mFragments.put(SettingItemMenu.GOOD_PRICE.getType(),mSetGoodPriceFragment);
        changeFragment(SettingItemMenu.PRINT_CONNECTION.getType());

        mSettingListview.setAdapter(new BaseCommAdapter<>(getActivity(), mItemData) {
            @Override
            public View getView(LeftItemBean data, View convertView, int postion) {
                SettingLeftItemView view1 = new SettingLeftItemView(getActivity());
                view1.setViewId(postion);
                settingLeftItemViews.add(view1);
                view1.showView(data.getName());
                if (postion == 0) {
                    view1.setTextColor(getResources().getColor(R.color.color_06AE56));
                } else {
                    view1.setTextColor(getResources().getColor(R.color.black));
                }
                if (postion==1){
                    view1.setbgColor();
                }
                else {
                    view1.setbgWhiteColor();
                }
                    view1.setOnClickListener(view -> {
                        if (postion == 0) {
                            return;
                        }
                        updateDataState(postion);
                        changeFragment(data.getId());

                    });

                return view1;
            }
        });
    }

    private void changeFragment(int fragmentType){
        mFragmentManager = getChildFragmentManager();
        mFragmentTransaction=mFragmentManager.beginTransaction();
        Fragment fragment=mFragments.get(fragmentType);
        if (fragment.isAdded()){
            mFragmentTransaction.show(fragment);
            return;
        }
        mFragmentTransaction.replace(R.id.mainContentView,fragment);
        mFragmentTransaction.commitAllowingStateLoss();
    }



    private void updateDataState(int pos) {
        for (SettingLeftItemView leftItemView : settingLeftItemViews) {
            if (leftItemView.getViewId()==pos) {
                leftItemView.setbgColor();
            } else {
                leftItemView.setbgWhiteColor();
            }
        }
    }

    private void initView() {
        versionName = mMainView.findViewById(R.id.versionName);
        mSettingListview = mMainView.findViewById(R.id.settingListview);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        try {
            mItemData.clear();
            mFragments.clear();
            removeFragment();
        }
        catch (Exception e){
        }

    }
    public void removeFragment() {
        for (Fragment fragment:mFragments.values()){
            if (fragment!=null){
                removeFragment(fragment);
            }
        }

    }
    private void removeFragment(Fragment fragment) {
        getActivity().getSupportFragmentManager().beginTransaction().remove(fragment).commitAllowingStateLoss();
    }


}
